package cn.aitrox.ry.service.image.controller;

import cn.aitrox.ry.bean.RespResult;
import cn.aitrox.ry.enumtype.RespCodeEnum;
import cn.aitrox.ry.service.image.api.ImageTransferApi;
import cn.aitrox.ry.service.image.dto.image.transfer.in.ImageTransferExecuteInDto;
import cn.aitrox.ry.service.image.dto.image.transfer.in.ImageTransferPauseInDto;
import cn.aitrox.ry.service.image.dto.image.transfer.in.ImageTransferRestartInDto;
import cn.aitrox.ry.service.image.dto.image.transfer.in.ImageTransferResultExportInDto;
import cn.aitrox.ry.service.image.dto.image.transfer.in.ImageTransferResultQueryInDto;
import cn.aitrox.ry.service.image.dto.image.transfer.out.ImageTransferResultExportDataDto;
import cn.aitrox.ry.service.image.dto.image.transfer.out.ImageTransferResultQueryOutDto;
import cn.aitrox.ry.service.image.service.ImageTransferService;
import cn.aitrox.ry.util.ArrayUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


@RestController
public class ImageTransferController implements ImageTransferApi {

    @Autowired
    private ImageTransferService imageTransferService;


    @RequestMapping("/image/transfer/result/query")
    public ImageTransferResultQueryOutDto resultQuery(@RequestBody ImageTransferResultQueryInDto input) {
        return imageTransferService.resultQuery(input.getTransferId(), input.getRelationType(), input.getMode());
    }

    @RequestMapping("/image/transfer/execute")
    public RespResult execute(@RequestBody ImageTransferExecuteInDto input) {
        RespResult respResult = this.validParam(input.getTransferId(), input.getRelationType(), input.getMode());
        if (!respResult._isOk()) {
            return respResult;
        }
        List<String> seriesUids = input.getSeriesUids().stream().distinct().collect(Collectors.toList());
        return imageTransferService.execute(input.getTransferId(), input.getRelationType(), input.getMode(), seriesUids);
    }

    @RequestMapping("/image/transfer/restart")
    public RespResult restart(@RequestBody ImageTransferRestartInDto input) {
        RespResult respResult = this.validParam(input.getTransferId(), input.getRelationType(), input.getMode());
        if (!respResult._isOk()) {
            return respResult;
        }
        return imageTransferService.restart(input.getTransferId(), input.getRelationType(), input.getMode());
    }

    @Override
    public RespResult pause(ImageTransferPauseInDto input) {
        RespResult respResult = this.validParam(input.getTransferId(), input.getRelationType(), input.getMode());
        if (!respResult._isOk()) {
            return respResult;
        }
        return imageTransferService.pause(input.getTransferId(), input.getRelationType(), input.getMode());
    }

    @RequestMapping("/image/transfer/result/export")
    public List<ImageTransferResultExportDataDto> resultExport(@RequestBody ImageTransferResultExportInDto input) {
        RespResult respResult = this.validParam(input.getTransferId(), input.getRelationType(), input.getMode());
        if (!respResult._isOk()) {
            return new ArrayList<>();
        }
        if(ArrayUtil.isEmpty(input.getSeriesUids())) {
            return new ArrayList<>();
        }
        return imageTransferService.resultExport(input.getTransferId(), input.getRelationType(), input.getMode(), input.getSeriesUids());
    }

    private RespResult validParam(String transferId, Integer relationType, String mode) {
        if (null == transferId || null == relationType || null == mode) {
            return RespResult.error(RespCodeEnum.PARAM_ERROR);
        }
        return RespResult.ok();
    }
}
